package com.atguigu.dga.governance.assess;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceMetric;
import com.atguigu.dga.meta.bean.TableMetaInfo;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public abstract class Assessor {
    protected abstract void assess(AssessParam assessParam,GovernanceAssessDetail governanceAssessDetail) throws ParseException;

    public final GovernanceAssessDetail mainAssess(AssessParam assessParam) {

        //设置默认值
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
//        assessParam.setGovernanceAssessDetail(governanceAssessDetail);
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setTableName(assessParam.getTableMetaInfo().getTableName());
        governanceAssessDetail.setSchemaName(assessParam.getTableMetaInfo().getSchemaName());
        governanceAssessDetail.setMetricId(assessParam.getGovernanceMetric().getId()+"");
        governanceAssessDetail.setMetricName(assessParam.getGovernanceMetric().getMetricName());
        governanceAssessDetail.setGovernanceType(assessParam.getGovernanceMetric().getGovernanceType());
        governanceAssessDetail.setTecOwner(assessParam.getTableMetaInfo().getTableMetaInfoExtra().getTecOwnerUserName());
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);

        //子类需要实现的考评方法
        try {
            assess(assessParam,governanceAssessDetail);
        }catch (Exception e){
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            governanceAssessDetail.setIsAssessException("1");
            int length = stringWriter.toString().length();
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0,Math.min(length,2000)));
        }

        governanceAssessDetail.setCreateTime(new Date());

        return governanceAssessDetail;
    }


}
